/* 
 * File:   main.c
 * Author: Marcel
 *
 * Created on 19. Juli 2012, 01:23
 */

#include <p18f2620.h>
#include <delays.h>

#include "indicator.h"
#include <stdlib.h>
#include <portb.h>
#include <usart.h>
#include "uart.h"
#include "vfd.h"
#include "onewire.h"
#include "timer.h"
#include "button.h"
#include "menu.h"
#include "heater.h"
#include "main.h"


/* This function WON'T initialize any specific hardware!
 * It only sets every single pin on the uC to a specific state at start. */
void pic_init(void) {
    #ifdef UART_OUTPUT
    uart_putrs((const far rom char*) "\n\rInitializing PIC");
    #endif

    // Set all RA ports to digital
    ADCON1 = 0b00001111;

    // Timer refer to external crystal
    OSCCONbits.SCS = 0;

    // Initialize Register A (1-Wire and VFD State Pins)
    TRISA = 0b00000001;
    LATA = 0b00000000;

    // Initialize Register B (VFD Data)
    TRISB = 0x00; // Data-Port -> out
    LATB = 0x00;

    // Initialize Register C (Heater, LEDs, Beeper, Buttons, RS232)
    TRISC = 0b10110000;
    LATC = 0b00000000;

    // Deactivate all internal Pullups on PORTB
    DisablePullups();

    // Interrupt setup
    RCONbits.IPEN = 1;  // Enable interrupt priority
    INTCONbits.GIEH = 1; // Enable all high priority interrupts
    INTCONbits.GIEL = 1;    // Enable all low priority interrupts
    #ifdef UART_OUTPUT
    uart_putrs((const far rom char*) "\t\t[DONE]");
    #endif
}


unsigned int temp_raw = 0;
void main(void) {
    uart_init();
    pic_init();
    idctr_init();
    button_init();
    timer_init();
    vfd_init();
    ow_init();
    menu_init();

    idctr_heater(0);
    while(1) {
        idctr_work();
        temp_raw = ow_work();
        
        menu_statemachine();

        vfd_draw();
        timer_flags();
    }
} 